/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： report-manage
 * 文件名称： ApiController.java
 * Date：18-11-5 下午2:21
 * Author: guomw
 */

package com.guomw.report.web.controller.api;

import com.guomw.report.common.enums.AppCode;
import com.guomw.report.common.utils.ApiResult;
import com.guomw.report.common.utils.RandomUtils;
import com.guomw.report.service.entity.SystemManager;
import com.guomw.report.service.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author guomw
 * @date 2018/11/5
 */
@RestController
@RequestMapping(value = "/api", method = RequestMethod.POST)
public class ApiController {
    @Autowired
    private SystemService systemService;

    @RequestMapping("/login")
    public ApiResult login(@RequestParam String loginName,
                           @RequestParam String loginPwd) {
        SystemManager manager = systemService.findManagerByLoginName(loginName);
        if (manager != null) {
            if (manager.getLoginPwd().toLowerCase().equals(loginPwd.toLowerCase())) {
                if (manager.isEnable()) {
                    manager.setLastLoginTime(LocalDateTime.now());
                    manager.setToken(RandomUtils.getUUID());
                    systemService.saveManager(manager);
                    return ApiResult.resultWith(AppCode.SUCCESS, "", manager.getToken());
                } else {
                    return ApiResult.resultWith(AppCode.SUCCESS, "账户已禁用，请联系管理员");
                }
            } else {
                return ApiResult.resultWith(AppCode.ERROR, "密码不正确");
            }
        }
        return ApiResult.resultWith(AppCode.ERROR, "用户不存在");
    }

    /***
     *
     * @param token
     * @param jsonData
     * @param file
     * @return
     */
    @RequestMapping("/uploadPdfReport")
    public ApiResult uploadPdfReport(@RequestParam String token,
                                     @RequestParam(value = "jsonData") String jsonData,
                                     @RequestParam(value = "img") MultipartFile file) {
        return ApiResult.resultWith(AppCode.ERROR, "用户不存在");
    }

}
